import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { environment } from '../../../../environments/environment';

@Component({
    selector: 'app-alarmanagement',
    templateUrl: './alarmanagement.component.html',
    styleUrls: ['./alarmanagement.component.less']
})

export class alarmanagementComponent implements OnInit {
    listOfData: any = [];
    listOfTemp: any = [];
    statnameValue = '';
    devnameValue = '';
    lockHeadScroll = { y: window.innerHeight - 350 + 'px' };

    constructor(private http: _HttpClient) { }

    ngOnInit() {
        this.getAlertInfo().subscribe((res: any) => {
            this.listOfTemp = res.response.data;
            this.listOfData = this.listOfTemp;
        });
    }

    // 报警信息
    getAlertInfo() {
        const url = environment.apiBase + '/csmis/alert/getInfo.jwt';
        return this.http.get(url);
    }

    reset(type): void {
        switch (type) {
            case 'statname':
                this.statnameValue = '';
                break;
            case 'devname':
                this.devnameValue = '';
                break;
        }
        this.search();
    }

    search(): void {
        const array = [];
        const statvalue = this.statnameValue.toUpperCase();
        const devalue = this.devnameValue.toUpperCase();

        if (!statvalue && !devalue) {
            this.listOfData = this.listOfTemp;
        } else {
            this.listOfTemp.forEach((item, index) => {
                const devname = JSON.stringify(item.DEVNAME);
                const statname = JSON.stringify(item.STATIONNAME);

                if (statvalue && devalue === '') {
                    if (statname.indexOf(statvalue) !== -1) {
                        this.getData(array, index);
                    }
                }
                if (statvalue === '' && devalue) {
                    if (devname.indexOf(devalue) !== -1) {
                        this.getData(array, index);
                    }
                }
                if (statvalue && devalue) {
                    if (statname.indexOf(statvalue) !== -1 && devname.indexOf(devalue) !== -1) {
                        this.getData(array, index);
                    }
                }
            });
            this.listOfData = array;
        }
    }
    getData(array, index) {
        array.push({
            STATIONNAME: this.listOfTemp[index].STATIONNAME,
            DEVNAME: this.listOfTemp[index].DEVNAME,
            ALARMTYPE: this.listOfTemp[index].ALARMTYPE,
            ALARMTEXT: this.listOfTemp[index].ALARMTEXT,
            TIME: this.listOfTemp[index].TIME
        });
        return array;
    }
}
